#pragma once

#include <AVUITriggerAction.h>
#include <AVUIObjectCollection.h>
#include <AVUIInheritanceContextHelper.h>

namespace AVUI {

class TriggerBase;

class TriggerActionCollection : public TObjectCollection<TriggerAction>
{
public:
    DECLARE_ELEMENT(TriggerActionCollection, ObjectCollectionBase);

    void Seal(TriggerBase* pContainingTrigger)
    {
        for(int idx = 0; idx < get_Count(); idx++)
        {
            (*this)[idx]->Seal(pContainingTrigger);
        }
    }

    DependencyObject* get_Owner() { return m_pOwner; }
    void set_Owner(DependencyObject* pOwner) { m_pOwner = pOwner; }

protected:

    virtual void ClearItems() AVUI_OVERRIDE
    {
        for (int i = get_Count() - 1; i >= 0; i--)
        {
            InheritanceContextHelper::RemoveContextFromObject(m_pOwner, (*this)[i]);
        }
        TObjectCollection<TriggerAction>::ClearItems();
    }

    virtual void InsertItem(int index, TriggerAction* pItem) AVUI_OVERRIDE
    {
        InheritanceContextHelper::ProvideContextForObject(m_pOwner, pItem);

        TObjectCollection<TriggerAction>::InsertItem(index, pItem);
    }

    virtual void RemoveItem(int index) AVUI_OVERRIDE
    {
        InheritanceContextHelper::RemoveContextFromObject(m_pOwner, (*this)[index]);

        TObjectCollection<TriggerAction>::RemoveItem(index);
    }

    virtual void SetItem(int index, TriggerAction* pItem) AVUI_OVERRIDE
    {
        if(pItem == (*this)[index])
        {
            return;
        }
        InheritanceContextHelper::RemoveContextFromObject(m_pOwner, (*this)[index]);
        InheritanceContextHelper::ProvideContextForObject(m_pOwner, pItem);

        TObjectCollection<TriggerAction>::SetItem(index, pItem);
    }

private:

    TWeakPtr<DependencyObject> m_pOwner;
};


}; // namespace AVUI
